package com.nbjtjc.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.jute.compiler.JFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RiskParse {
    public static void main(String[] args) throws IOException {
        Document d= Jsoup.connect("http://www.safetyhua.com/Public/Login").get();
        //获取仿登录的数据
        d.getElementById("AccountCode").attr("value","15314594898");
        d.getElementById("Password").attr("value","cqh888888");
        //用map来存储数据
        Map map=new HashMap();
        map.put("AccountCode","00274_cqh");
        map.put("Password","cqh888888");

        //登录，获取cookies
        Connection.Response login=Jsoup.connect("http://www.safetyhua.com/Public/LoginPost")
                .ignoreContentType(true)
                .followRedirects(true)
                .data(map)
                .method(Connection.Method.POST)
                .execute()
                .charset("utf-8");

        //模拟成功后，自己想获取的数据，并且要求携带cookies数据
//        String url= URLEncoder.encode("http://www.safetyhua.com/RiskPointLib/GetPageData?dataType=sys&Profession=IAPT00007,IAPT00023,IAP00008&order=asc&offset=0&limit=50&page=0&conditions=[{\"Operator\":\"=\",\"DataType\":\"string\",\"Name\":\"EvaluationMethod\",\"Value\":\"\"},{\"Name\":\"RiskPointName\",\"Value\":\"\"}]&_=1611384037180","utf-8");
        String url= URLEncoder.encode("http://www.safetyhua.com/RiskPointLib/GetPageData" +
                "?companyCode=00274&dataType=company&order=asc&offset=0&limit=50&page=0","utf-8");

        URL uuu=new URL("http://www.safetyhua.com/RiskPointLib/GetPageData?dataType=sys&Profession=IAPT00007,IAPT00023,IAP00008&order=asc&offset=0&limit=500&page=0&conditions=[{\"Operator\":\"=\",\"DataType\":\"string\",\"Name\":\"EvaluationMethod\",\"Value\":\"\"},{\"Name\":\"RiskPointName\",\"Value\":\"\"}]&_=1611384037180");
//        Document index=Jsoup.connect("http://www.safetyhua.com/RiskPointLib/GetPageData?dataType=sys&Profession=IAPT00007,IAPT00023,IAP00008&order=asc&offset=0&limit=500&page=0&conditions=%5B%7B%22Operator%22%3A%22%3D%22%2C%22DataType%22%3A%22string%22%2C%22Name%22%3A%22EvaluationMethod%22%2C%22Value%22%3A%22%22%7D%2C%7B%22Name%22%3A%22RiskPointName%22%2C%22Value%22%3A%22%22%7D%5D&_=1611384037180")
//                .ignoreContentType(true).cookies(login.cookies()).get();
        Document index=Jsoup.connect("http://www.safetyhua.com/RiskPointLib/GetPageData?companyCode=00274&dataType=company&order=asc&offset=0&limit=300&page=0&conditions=%5B%7B%22Operator%22%3A%22%3D%22%2C%22DataType%22%3A%22string%22%2C%22Name%22%3A%22EvaluationMethod%22%2C%22Value%22%3A%22%22%7D%2C%7B%22Name%22%3A%22RiskPointName%2CKeyWord%2CRemarks%22%2C%22Value%22%3A%22%22%7D%5D&_=1611548220842")
                .ignoreContentType(true).cookies(login.cookies()).get();
        //打印数据网页
        System.out.println(index.body().text());
        String jsonString= index.body().text();
        JSONObject riskPoints= JSON.parseObject(jsonString);
        JSONArray riskListJson=riskPoints.getJSONArray("data");
        String riskItemUrl="http://www.safetyhua.com/RiskPointIdentifyLib/GetPageData?companyCode=00274&dataType=company" +
                "&order=asc" +
                "&offset=0&limit=500&page=0&";
        List<RiskItem> riskItemList=new ArrayList<>();
        for(int i=0;i<riskListJson.size();i++){
            JSONObject riskJson=riskListJson.getJSONObject(i);
            Document riskItemDoc=Jsoup.connect(riskItemUrl+"rpCode="+riskJson.getString("RPCode")).ignoreContentType(true)
                    .cookies(login.cookies()).get();
            JSONObject itemJson=JSON.parseObject(riskItemDoc.child(0).getElementsByTag("body").text());
            JSONArray itemArrayJson= itemJson.getJSONArray("data");
            for(int j=0;j<itemArrayJson.size();j++){
                RiskItem riskItem=itemArrayJson.getJSONObject(j).toJavaObject(RiskItem.class);
                riskItem.setRPTypeName(riskJson.getString("RPTypeName"));
                riskItemList.add(riskItem);
//                System.out.println(riskItem.toString());
            }

        }
        generateExcel(riskItemList);
    }

    public  static void generateExcel(List<RiskItem> riskItemList) throws IOException {
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook();
        XSSFSheet xssfSheet=xssfWorkbook.createSheet("fxfjgk");
        int rowIndex=0;
        int cellIndex=1;
        Row nRow=null;
        Cell nCell=null;
        for(int i=0;i<riskItemList.size();i++){
            RiskItem riskItem=riskItemList.get(i);
            nRow=xssfSheet.createRow(rowIndex++);
            cellIndex=0;
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getRiskPointName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getRPTypeName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getActivity());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getDangerousSource());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getLatentConsequencesName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getAccidentName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationCalcuName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationType());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationValue());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationBase());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getControlLevel());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getInducedCause());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getControlMeasures());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getLawReference());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getManageMeasures());
            System.out.println(riskItem.getManageMeasures());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getTrainMeasures());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEmergencyMeasures());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationResultType());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationResultValue());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationResultName());
            nCell=nRow.createCell(cellIndex++);
            nCell.setCellValue(riskItem.getEvaluationResultBase());
        }
//        File excel=new File("/Users/guqianbin/Desktop/fxfjgk.xlsx");
        File excel=new File("d:\\fxfjgk.xlsx");
        if(excel.exists()){
            excel.delete();
        }
        excel.createNewFile();
        FileOutputStream fileOutputStream=new FileOutputStream(excel);
        xssfWorkbook.write(fileOutputStream);
        fileOutputStream.close();
    }

}
